﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// Expression表达式扩展操作类
    /// </summary>
    public static class ExpressionExtensions
    {
        #region 方法
        /// <summary>
        /// 以特定的条件运行组合两个Expression表达式
        /// </summary>
        /// <typeparam name="T">表达式的主实体类型</typeparam>
        /// <param name="first">第一个Expression表达式</param>
        /// <param name="second">要组合的Expression表达式</param>
        /// <param name="merge">组合条件运算方式</param>
        /// <returns>组合后的表达式</returns>
        public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
        {
            Check.NotNull(first, nameof(first));
            Check.NotNull(second, nameof(second));
            Check.NotNull(merge, nameof(merge));

            Dictionary<ParameterExpression, ParameterExpression> map =
                first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
            Expression secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
            return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
        }

        /// <summary>
        /// 以 Expression.AndAlso 组合两个Expression表达式
        /// </summary>
        /// <typeparam name="T">表达式的主实体类型</typeparam>
        /// <param name="first">第一个Expression表达式</param>
        /// <param name="second">要组合的Expression表达式</param>
        /// <param name="ifExp">判断条件表达式，当此条件为true时，才执行组合</param>
        /// <returns>组合后的表达式</returns>
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, bool ifExp = true)
        {
            Check.NotNull(first, nameof(first));
            Check.NotNull(second, nameof(second));
            return ifExp ? first.Compose(second, Expression.AndAlso) : first;
        }

        /// <summary>
        /// 以 Expression.OrElse 组合两个Expression表达式
        /// </summary>
        /// <typeparam name="T">表达式的主实体类型</typeparam>
        /// <param name="first">第一个Expression表达式</param>
        /// <param name="second">要组合的Expression表达式</param>
        /// <param name="ifExp">判断条件表达式，当此条件为true时，才执行组合</param>
        /// <returns>组合后的表达式</returns>
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, bool ifExp = true)
        {
            Check.NotNull(first, nameof(first));
            Check.NotNull(second, nameof(second));
            return ifExp ? first.Compose(second, Expression.OrElse) : first;
        }

        /// <summary>
        /// 转换条件表达式谓语参数类型
        /// </summary>
        /// <typeparam name="TSource">要转换的源参数类型</typeparam>
        /// <typeparam name="TTarget">要转换的目标参数类型</typeparam>
        /// <param name="predicate">转换前的谓语条件表达式</param>
        /// <returns>转换后的谓语条件表达式</returns>
        public static Expression<Func<TTarget, bool>> ConvertPredicateParameterType<TSource, TTarget>(this Expression<Func<TSource, bool>> predicate)
            where TSource : class
            where TTarget : class
        {
            Check.NotNull(predicate, nameof(predicate));

            ExpressionObjectConverter<TTarget> converter = new();
            var lamdba = converter.Modify(predicate);
            return lamdba as Expression<Func<TTarget, bool>>;
        }

        #endregion

        //私有类
        #region 参数重新绑定
        /// <summary>
        /// 参数重新绑定
        /// </summary>
        private class ParameterRebinder : ExpressionVisitor
        {
            private readonly Dictionary<ParameterExpression, ParameterExpression> _map;

            private ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
            {
                _map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
            }

            public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
            {
                return new ParameterRebinder(map).Visit(exp);
            }

            protected override Expression VisitParameter(ParameterExpression node)
            {
                if (_map.TryGetValue(node, out ParameterExpression replacement))
                {
                    node = replacement;
                }
                return base.VisitParameter(node);
            }
        }
        #endregion

    }
}